<template>
  <el-container class="home-container">
    <!-- 侧边栏区域 -->
    <el-aside :width="isCollapse ? '64px' : '200px'">
      <!-- 侧边栏菜单区域 -->
      <el-menu active-text-color="#409eff" background-color="#304156" text-color="#fff"
        :unique-opened="true" :collapse="isCollapse" :collapse-transition="false" :router="true"
        :default-active="activePath">
        <!-- 一级菜单 -->
        <el-submenu :index="item.id+''" v-for="item in permissions" :key="item.id"
          v-show="item.parentId === null">
          <!-- 一级菜单的模板区 -->
          <template #title>
            <!-- <el-icon>
              <location />
            </el-icon> -->
            <!-- 图标 -->
            <span class="iconfont" v-html="iconsObj[item.id]">></span>
            <!-- 文本 -->
            <span>{{ item.name }}</span>
          </template>

          <!-- 二级菜单 -->
          <el-menu-item :index="it.url" v-for="it in permissions" :key="it.id"
            v-show="item.id === it.parentId" @click="saveNavState(it.url)">
            <template #title>
              <!-- 图标 -->
              <span class="iconfont">&#xe6ec;</span>
              <!-- 文本 -->
              <span>{{ it.name }}</span>
            </template>

          </el-menu-item>
        </el-submenu>

      </el-menu>
    </el-aside>
    <el-container>
      <!-- 头部区域 -->
      <el-header>
        <i class="font-icon"><span class="iconfont toggle-button"
            @click="toggleCollapse">&#xe621;</span></i>

        <el-breadcrumb>
          <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>用户管理</el-breadcrumb-item>
          <el-breadcrumb-item>用户列表</el-breadcrumb-item>
        </el-breadcrumb>
        <el-button type="info" @click="logout" class="exit">退出</el-button>

      </el-header>
      <!-- 内容主体 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>

</template>

<script>
export default {
  name: '',
  data () {
    return {
      isShow: '',
      permissions: [],
      iconsObj: {
        '1': '&#xe614;',
        '8': '&#xe618;',
        '15': '&#xe8ae;'
      },
      isCollapse: false,
      // 被激活的连接地址
      activePath: ''
    }
  },
  // 生命周期函数
  created () {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout () {
      this.$http({
        method: 'GET',
        url: 'api/user/exit',
        headers: {
          token: window.sessionStorage.getItem('token')
        }
      })
        .then((res) => {
          console.log(res.data)
          this.$router.push('/login')
          window.sessionStorage.clear()
          this.$message.success('退出成功')
        })
        .catch((res) => {
          console.log(res)
        })
    },
    // 获取所有的菜单
    getMenuList () {
      // 从sessionStorage中获取当前用户的权限菜单
      let permission = window.sessionStorage.getItem('permissions')
      this.permissions = JSON.parse(permission)
      console.log(this.permissions)
    },
    // 点击按钮切换折叠和展开
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    // 保存连接的激活状态
    saveNavState (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}

.el-header {
  background-color: #ffffff;
}

.el-aside {
  background-color: #304156;
  .el-menu {
    border-right: none;
  }
}

.el-main {
  background-color: #f5f7fa;
}

.iconfont {
  margin-right: 3px;
}

.font-icon {
  display: inline-block;
  vertical-align: middle;
  // height: 100%;
  .toggle-button {
    position: absolute;
    font-size: 23px;
    text-align: center;
    cursor: pointer;
  }
}

.exit {
  position: absolute;
  right: 20px;
  top: 10px;
}

.el-breadcrumb {
  position: absolute;
  left: 260px;
  top: 18px;
}
</style>
